<?php

class Jf_Utility_Dir {

    public static function getSubDir ($dir) {
        if ( ! file_exists ($dir)) {
            return array();
        }
        $subDirs = array();
        $dirIterator = new DirectoryIterator ($dir);
        foreach ($dirIterator as $dir) {
            if ($dir -> isDot () ||  ! $dir -> isDir ()) {
                continue;
            }
            $dir = $dir -> getFilename ();
            if ($dir == '.svn') {
                continue;
            }
            $subDirs[] = $dir;
        }
        return $subDirs;
    }

    public static function getRecursiveDir ($dir, $maxDepth = null) {
        $dirs = '';
        $dir = new RecursiveDirectoryIterator ($dir, RecursiveDirectoryIterator::KEY_AS_FILENAME | FilesystemIterator::SKIP_DOTS);
        $iterator = new RecursiveIteratorIterator ($dir, RecursiveIteratorIterator::SELF_FIRST);
        $iterator -> setMaxDepth ($maxDepth);
        foreach ($iterator as $filename => $dir) {
            $indent = str_repeat ('&nbsp;&nbsp;', $iterator -> getDepth ());
            $dirs .= "<option value=\"$filename\">" . $indent . $filename . '</option>';
        }
        return $dirs;
    }
    // $iterator = new RecursiveTreeIterator($dir, RecursiveIteratorIterator::SELF_FIRST);
    // self::unicodeTreePrefix($iterator);
    public function unicodeTreePrefix (RecursiveTreeIterator $tree) {
        $prefixParts = array(RecursiveTreeIterator::PREFIX_LEFT => ' ', RecursiveTreeIterator::PREFIX_MID_HAS_NEXT => '│ ', RecursiveTreeIterator::PREFIX_END_HAS_NEXT => '├ ', RecursiveTreeIterator::PREFIX_END_LAST => '└ ');
        foreach ($prefixParts as $part => $string) {
            $tree -> setPrefixPart ($part, $string);
        }
    }

    public static function deleteRescursiveDir ($dir) {
        if (is_dir ($dir)) {
            $dir = (substr ($dir,  - 1) != DS) ? $dir . DS : $dir;
            $openDir = opendir ($dir);
            while ($file = readdir ($openDir)) {
                if ( ! in_array ($file, array(".", ".."))) {
                    if ( ! is_dir ($dir . $file)) {
                        @unlink ($dir . $file);
                    } else {
                        self::deleteRescursiveDir ($dir . $file);
                    }
                }
            }
            closedir ($openDir);
            @rmdir ($dir);
        }
        return true;
    }

    public static function copyRescursiveDir ($source, $dest) {
        $openDir = opendir ($source);
        if ( ! file_exists ($dest)) {
            @mkdir ($dest);
        }
        while ($file = readdir ($openDir)) {
            if ( ! in_array ($file, array(".", ".."))) {
                if (is_dir ($source . DS . $file)) {
                    self::copyRescursiveDir ($source . DS . $file, $dest . DS . $file);
                } else {
                    copy ($source . DS . $file, $dest . DS . $file);
                }
            }
        }
        closedir ($openDir);
        return true;
    }

    public static function createDirs ($root, $path) {
        $root = rtrim ($root, DS);
        $subDirs = explode (DS, $path);
        if ($subDirs == null) {
            return;
        }
        $currDir = $root;
        foreach ($subDirs as $dir) {
            $currDir = $currDir . DS . $dir;
            if ( ! file_exists ($currDir)) {
                mkdir ($currDir);
            }
        }
    }

}
